<template>
	<view class="sign-page">
		<!-- 签到信息 -->
		<view class="sign-info">
			<text v-if="currentDay" style="font-size: 40rpx;">今日第{{ currentDay }}个签到</text>
			<text v-if="remainDays > 0">距离明天签到还有{{ remainDays }}天</text>
		</view>
		<!-- 签到按钮 -->
		<view v-if="!this.signList.includes(this.today)" class="sign-button" @click="handleSignIn">点击签到</view>
		<view v-if="this.signList.includes(this.today)" class="end-button" @click="handleSignIn">您已签到</view>
		<!-- 连续签到信息 -->
		<view class="continuous-sign">
			<text>连续签到{{ continuousDays }}天</text>
		</view>
		<!-- 日历组件 -->
		<qian-dao :list="signList" :date="date" :time_key="time_key" @day_change="day_change_fun" @date_change="date_change_fun"></qian-dao>
		<text style="font-size: 40rpx;margin-top: 40rpx;">
			<u-icon name="integral" size="38" style="margin:0 20rpx;"></u-icon>
			签到积分: {{userInfo.integral}}</text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentDay: 0, // 当前是第几天签到
				remainDays: 0, // 距离明天签到还有几天
				continuousDays: 0, // 连续签到天数
				missedDays: 0, // 漏签天数
				// 当前的日期
				date: "",

				// 存放时间的key值
				time_key: "",
				// 已经签到的数据列表
				userInfo: "",
				signList: [],
				today:new Date().toISOString().slice(0, 10),
			};
		},
		onLoad() {
			this.getUserInfo();
		},
		methods: {
			getUserInfo() {
				this.$H.get('user/userInfo').then(res => {
					this.userInfo = res.result;
					// 通过uid获取签到日历
					this.$H.get('sign/list', {
						userId: this.userInfo.uid
					}).then(res => {
						this.continuousDays = res.result[res.result.length-1].conDays;
						res.result.forEach(item => {
							const datePart = item.signDate.substring(0, 10); // 提取日期部分
							this.signList.push(datePart);
						});

					});
					// 获取签到排行
					this.$H.get('sign/today', {
						userId: this.userInfo.uid
					}).then(res => {
						this.currentDay = res.result
					})
				});
			},
			// 点击天
			day_change_fun(day) {
				console.log(JSON.parse(JSON.stringify(day)));

				// 存放时间的key值
				let time_key = this.time_key;

				// 如果没有签到（可以补签，需要补签的可以这么写）
				if (!day.click) {
					if (time_key) {
						let obj = {};
						obj[time_key] = day.nyr;
						this.list.push(obj);
					} else {
						this.list.push(day.nyr);
					}
				}

				// 如果今天没有签到(只签到今天的，如果只需要签到今天的可以这么写)
				// if (!day.click && day.type == "today") {
				//  if (time_key) {
				//      let obj = {};
				//      obj[time_key] = day.nyr;
				//      this.list.push(obj);
				//  } else {
				//      this.list.push(day.nyr);
				//  }
				// }
			},
			// 日期改变时触发
			date_change_fun(date) {
				// 更新日期
				this.date = date;

				// 根据日期获取已经签到的列表然后赋值
				// 存放时间的key值
				let time_key = this.time_key;
				if (time_key) {
					this.list.push({
						time: "01"
					});
				} else {
					this.list.push("01");
				}
			},
			 getCurrentPageUrl() {
			    let pages = getCurrentPages();
			    let currentPage = pages[pages.length - 1];
			    return currentPage.route;
			},
			handleSignIn() {
				if (this.signList.includes(this.today)) {
					this.$u.toast('今天已签到');
				} else {
					this.$H.get('sign/signin', {
						userId: this.userInfo.uid
					}).then(res=>{
						this.$u.toast('签到成功');
						let currentPage = getCurrentPageUrl();
						console.log(currentPage)
						uni.redirectTo({
							url: currentPage
						});
					})
					this.getUserInfo();
				}
			}
		}
	}
</script>

<style lang="scss">
	/* 样式可以根据需要自行修改 */
	.sign-page {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.sign-info {
		margin-top: 20px;
	}

	.sign-button {
		margin: 20px 0;
		padding: 10px 20px;
		background-color: #007bff;
		color: white;
		border-radius: 5px;
		cursor: pointer;
	}
	.end-button {
		margin: 20px 0;
		padding: 10px 20px;
		background-color: #aa0000;
		color: white;
		border-radius: 5px;
		cursor: pointer;
	}
	.continuous-sign {
		margin-bottom: 20px;
	}

	.calendar {
		/* 日历组件样式 */
	}
</style>
